//------------------------------------------------------------------------------
// Fichiers d'ent�te utilis�s
//------------------------------------------------------------------------------
#include "RefiningOperator.hpp"
#include "Tools/Logger.hpp"

//------------------------------------------------------------------------------
// Définition de constantes
//------------------------------------------------------------------------------

namespace ImageAnalysis
{

	//----------------------------------------------------------------------
	// Constructeur
	//----------------------------------------------------------------------
	RefiningOperator::RefiningOperator( BinaryImage* originalImage, Gradiant* gradiant, BinaryImage* refinedImage ):
	currentOriginalImage(originalImage),
	currentGradiant(gradiant),
	currentRefinedImage(refinedImage)
	{

	}

	//----------------------------------------------------------------------
	// Destructeur
	//----------------------------------------------------------------------
	RefiningOperator::~RefiningOperator( )
	{

	}

	//----------------------------------------------------------------------
	//
	//----------------------------------------------------------------------
	void RefiningOperator::Process( )
	{
		int imageSize = currentOriginalImage->GetHeight() * currentOriginalImage->GetWidth();

		for( int pixelIndex=0; pixelIndex < imageSize; ++pixelIndex )
		{
			if(  *currentOriginalImage->GetPixel(pixelIndex) == BinaryImage::WHITE_VALUE )
			{
				Image::PixelNeighborHood& neighborHood    = currentOriginalImage->GetDirectPixelNeighborHood(pixelIndex);
				Orientation::TDirection direction  	  = currentGradiant->GetGradiantApproximatedOrientation(pixelIndex);
				Orientation::TDirection oppositeDirection = Orientation::GetOppositeDirection(direction);

				//float angleDir = currentGradiant->GetGradiantOrientation( pixelIndex );
				//Tools::ILogger::Log("index : %d, direction:%u / %f, directionOpp:%u  \n", pixelIndex,direction, angleDir, oppositeDirection );
				//Tools::ILogger::Log("indiceN1:%u, indiceN2:%u\n", neighbor1,neighbor2);
				int neighbor1 = neighborHood.index[direction];
				int neighbor2 = neighborHood.index[oppositeDirection];

				if( (neighbor1 != -1) && (neighbor2 != -1) )
				    //(*currentOriginalImage->GetPixel(neighbor1) == BinaryImage::WHITE_VALUE ) &&
				    //(*currentOriginalImage->GetPixel(neighbor2) == BinaryImage::WHITE_VALUE ) )
				{
					
					//Tools::ILogger::Log("%u,%u,%u\n",currentGradiant->GetGradiantNorm(neighbor1),currentGradiant->GetGradiantNorm(neighbor2),currentGradiant->GetGradiantNorm(pixelIndex) );
					unsigned char value;
					if( (currentGradiant->GetGradiantNorm(neighbor1) <= currentGradiant->GetGradiantNorm(pixelIndex) ) &&
					    (currentGradiant->GetGradiantNorm(neighbor2) < currentGradiant->GetGradiantNorm(pixelIndex) ) )
					{
						value = BinaryImage::WHITE_VALUE;
					}
					else
					{
						value = BinaryImage::BLACK_VALUE;
					}					
					currentRefinedImage->SetPixel( pixelIndex, &value );
				}
			}
			else
			{
				unsigned char value = BinaryImage::BLACK_VALUE;
				currentRefinedImage->SetPixel( pixelIndex, &value );
			}
		}
	}
}

